import http from '../../../http/http'
import * as url from '../../../api/cfApi'
import {Message} from 'element-ui';
//餐桌页面数据存放
const state = {
  showPage: 1,
  seatsList: [],
  peopleNumList: [],
  typeList: [],
  stateList: [
    {value: 1, desc: '可用'},
    {value: 2, desc: '占用'},
    {value: 3, desc: '停用'},
    {value: 4, desc: '脏台'}
  ],
  length: 0,
}
const mutations = {
  seatsStore(state,seats) {
    state.seatsList = seats
    state.seatsList.forEach(item => {
      //人数的分类
      if (!state.peopleNumList.includes(item.peopleNum)) {
        state.peopleNumList.push(item.peopleNum)
      }
      //餐桌状态的分类
      if (!state.typeList.includes(item.type)) {
        state.typeList.push(item.type)
      }
    })
  },
  changePageMU(state,page) {
    state.showPage =page
  },
  seatDownMU(state,number) {
    state.seatedNum = number
  }
}
const actions = {
  seatsAC({commit},obj) {
    console.log('获取餐桌的信息',obj)
    return http({
      method: 'post',
      url: '/api'+url.SEATS_SELECT,
      data: obj
    }).then(res => {
      console.log('餐桌数据获取成功',res)
      if (res.data.code ==='200') {
        commit('seatsStore',res.data.seatsList)
        return res.data.seatsList
      }
      else Message.error('餐桌数据获取失败')
    }).catch(err => {
      console.log(err)
    })
  },
  // changePageAC({commit},page) {
  //   console.log(page)
  //   commit('changePageMU',page)
  // },
  changeSeatState({commit},obj) {
    console.log('改变餐桌的状态',obj )
    return http({
      url: '/api'+url.CHANGE_SEAT_STATE,
      method: 'post',
      data: obj
    }).then(res => {
      console.log('餐桌状态改变',res)
      if (res.status === 200) {
        if (res.data.code === '200') { // 有桌台可以清空
          commit('seatsStore',res.data.seatsList)
          
        } else if (res.data.code === '500') {
          Message.info('桌台都出与空闲状态,放心使用,不必清空')
        }
        return res.data
      }
      
    }).catch(err => {
      console.log(err)
    })
  },
  seatDownAC({commit},val){
    console.log('坐下了',val)
    commit('seatDownMU',val)
  }
};


const  getters= {
  showSeatsArr(state) {
    return state.seatsList.filter((item,index) => index>= (state.showPage-1)*12 && index<state.showPage*12)
  },
  seatLen(state) {
    return state.seatsList.length
  }
};
export default {
  namespaced: true,//此属性将getters,actions,mutations局部化
  state,
  actions,
  mutations,
  getters
}

//
//
// let 菜品top3 = {
//   code: 200,
//   token: xxxxxxx,
//   data: [
//     {
//       dishName: '鱼香肉丝',
//       count: 100
//     },
//     {
//       dishName: '小炒肉',
//       count: 99
//     },
//   ]
// }
//
// let 翻桌 = {
//
// }
